from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse
from .models import Post
import markdown
from comments.forms import CommentForm
from django.views.generic import ListView
from .models import Category ,Tag
from django.db.models import Q

def index(request):
    post_list = Post.objects.all() #- 号表示逆序
    return render(request, 'blog/index.html', context={
        'post_list':post_list
    })


def detail(request, pk):
    post = get_object_or_404(Post, pk=pk)

    post.increase_view()
    post.body = markdown.markdown(post.body,
                                  extensions=[
                                      'markdown.extensions.extra',
                                      'markdown.extensions.codehilite',
                                      'markdown.extensions.toc',
                                      'markdown.extensions.codehilite',
                                  ])

    form = CommentForm()
    comment_list = post.comment_set.all()
    context = {
            'post' : post,
            'form' : form,
            'comment_list' : comment_list
    }
    return render(request, 'blog/detail.html', context=context)

#获取归档文章
def archives(request, year, month):
    post_list = Post.objects.filter(created_time__year=year,
                                    created_time__month=month
                                    ).order_by('-created_time')
    return render(request, 'blog/index.html', context={'post_list': post_list})


#获取标签的文章
def category(request, pk):
    # 记得在开始部分导入 Category 类
    cate = get_object_or_404(Category, pk=pk)
    post_list = Post.objects.filter(category=cate).order_by('-created_time')
    return render(request, 'blog/index.html', context={'post_list': post_list})



#类视图
class IndexView(ListView):
    model = Post
    template_name = 'blog/index.html'
    context_object_name = 'post_list'
    # 指定 paginate_by 属性后开启分页功能，其值代表每一页包含多少篇文章
    paginate_by = 2



class CategoryView(ListView):
    model = Post
    template_name = 'blog/index.html'
    context_object_name = 'post_list'

    def get_queryset(self):
        cate = get_object_or_404(Category, pk=self.kwargs.get('pk'))
        return super(CategoryView, self).get_queryset().filter(category=cate)



class ArchivesView(ListView):
    model = Post
    template_name = 'blog/index.html'
    context_object_name = 'post_list'

    def get_queryset(self):
        post_list = Post.objects.filter(created_time__year=self.kwargs.get('year'),
                                        created_time__month=self.kwargs.get('year')
                                        ).order_by('-created_time')
        return super(ArchivesView, self).get_queryset()


class TagView(ListView):
    model = Post
    template_name = 'blog/index.html'
    context_object_name = 'post_list'

    def get_queryset(self):
        tag = get_object_or_404(Tag, pk=self.kwargs.get('pk'))
        return super(TagView, self).get_queryset().filter(tags=tag)



def search(request):
    q = request.GET.get('q')
    error_msg = ''

    if not q:
        error_msg = "请输入关键词"
        return render(request, 'blog/index.html', {'error_msg': error_msg})

    post_list = Post.objects.filter(Q(title__icontains=q) | Q(body__icontains=q))
    return render(request, 'blog/index.html', {'error_msg': error_msg,
                                               'post_list': post_list})